<template>
  <div>
    <el-button
      type="primary"
      plain
      size="small"
      icon="el-icon-plus"
      style="float: left"
      @click="show"
      >新增</el-button
    >
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入企业名称"
      v-model="nickname"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入企业账号"
      v-model="username"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入企业电话号"
      v-model="mobile"
    />
    <el-input
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      placeholder="请输入企业邮箱"
      v-model="email"
    />
    <el-select
      v-model="quality"
      style="width: 200px; float: left; margin-left: 15px"
      size="small"
      clearable
      placeholder="请选择企业性质"
    >
      <el-option
        v-for="item in options"
        :key="item"
        :label="item"
        :value="item"
      >
      </el-option>
    </el-select>
    <el-button
      type="primary"
      icon="el-icon-search"
      style="margin-left: 15px"
      size="small"
      @click="initEeInfo"
      >搜索</el-button
    >
    <el-button icon="el-icon-refresh" size="small" @click="clearInput"
      >重置</el-button
    >
    <el-button
      style="
        float: left;
        margin-top: 10px;
        margin-right: 15px;
        margin-left: 0px;
      "
      type="danger"
      plain
      icon="el-icon-delete"
      size="small"
      @click="delAll"
      :disabled="this.multipleSelection.length == 0"
      >批量删除</el-button
    >
    <el-upload
      style="float: left; margin-top: 10px"
      action="/enterprise/import"
      :headers="headers"
      :show-file-list="false"
      :on-success="handleAvatarSuccess4E"
      :before-upload="beforeAvatarUpload4E"
    >
      <el-button type="info" plain size="small" icon="el-icon-download"
        >导入</el-button
      >
    </el-upload>
    <el-button
      type="warning"
      plain
      size="small"
      icon="el-icon-upload2"
      style="float: left; margin-left: 15px; margin-top: 10px"
      @click="exportEeInfo"
      >导出</el-button
    >
    <el-table
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%; margin-top: 54px"
      :cell-style="rowClass"
      :header-cell-style="headClass"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="45"> </el-table-column>
      <el-table-column prop="nickname" label="企业名称" width="100">
        <template slot-scope="scope"
          ><el-popover placement="bottom" width="150" trigger="hover"
            ><div>
              <h4>企业logo</h4>
              <img style="width: 150px" :src="scope.row.logo" />
              <h4>企业描述</h4>
              <font style="font-size: 10px">{{ scope.row.description }}</font>
            </div>
            <el-tag slot="reference">{{
              scope.row.nickname
            }}</el-tag></el-popover
          ></template
        >
      </el-table-column>
      <el-table-column prop="username" label="企业账号" width="160">
      </el-table-column>
      <el-table-column prop="mobile" label="企业电话号" width="200">
      </el-table-column>
      <el-table-column prop="email" label="企业邮箱" width="200">
      </el-table-column>
      <el-table-column prop="quality" label="企业性质" width="110">
      </el-table-column>
      <el-table-column prop="enabled" label="是否启用" width="110">
        <template slot-scope="scope">
          <el-switch
            v-if="scope.row.enabled"
            v-model="scope.row.enabled"
            @change="changeEnabled(scope.row.id, scope.row.nickname)"
          ></el-switch>
          <el-switch
            v-else
            v-model="scope.row.enabled"
            @change="changeDisable(scope.row.id)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" width="200">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 1.5%; float: right">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="currentPage"
        :page-sizes="[5, 8]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="添加企业"
      :visible.sync="dialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="formData"
        ref="registerForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="nickname" label="企业名称" size="mini">
          <el-input
            v-model="formData.nickname"
            placeholder="请输入企业名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="username" label="企业账号" size="mini">
          <el-input
            v-model="formData.username"
            placeholder="请输入企业账号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" size="mini">
          <el-input
            v-model="formData.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="电话号" size="mini">
          <el-input
            v-model="formData.mobile"
            placeholder="请输入电话号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" size="mini">
          <el-input
            v-model="formData.email"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item prop="address" label="地址" size="mini">
          <el-input
            v-model="formData.address"
            placeholder="请输入地址"
          ></el-input>
        </el-form-item>
        <el-form-item prop="quality" label="企业性质" size="mini">
          <el-select
            v-model="formData.quality"
            clearable
            placeholder="请选择企业性质"
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="logo" label="企业logo" size="mini">
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :before-upload="beforeAvatarUpload"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="mini" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item prop="description" label="公司简介" size="mini">
          <el-input
            type="textarea"
            autosize
            v-model="formData.description"
            placeholder="请输入公司简介"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="dialogVisible = false">取消</el-button>
        <el-button size="mini" type="primary" @click="addEe">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="编辑企业"
      :visible.sync="editDialogVisible"
      width="400px"
      center
    >
      <el-form
        :model="editData"
        ref="editForm"
        label-width="80px"
        :rules="rules"
      >
        <el-form-item prop="nickname" label="企业名称" size="mini">
          <el-input
            v-model="editData.nickname"
            placeholder="请输入企业名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码" size="mini">
          <el-input
            v-model="editData.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="电话号" size="mini">
          <el-input
            v-model="editData.mobile"
            placeholder="请输入电话号"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" size="mini">
          <el-input
            v-model="editData.email"
            placeholder="请输入邮箱"
          ></el-input>
        </el-form-item>
        <el-form-item prop="address" label="地址" size="mini">
          <el-input
            v-model="editData.address"
            placeholder="请输入地址"
          ></el-input>
        </el-form-item>
        <el-form-item prop="quality" label="企业性质" size="mini">
          <el-select
            v-model="editData.quality"
            clearable
            placeholder="请选择企业性质"
          >
            <el-option
              v-for="item in options"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="logo" label="企业logo" size="mini">
          <el-upload
            class="upload-demo"
            action="/enterprise/logo"
            :show-file-list="false"
            :headers="headers"
            :before-upload="beforeAvatarUpload"
            :on-success="handleAvatarSuccess"
          >
            <el-button size="mini" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item prop="description" label="公司简介" size="mini">
          <el-input
            type="textarea"
            autosize
            v-model="editData.description"
            placeholder="请输入公司简介"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button size="mini" @click="editDialogVisible = false"
          >取消</el-button
        >
        <el-button size="mini" type="primary" @click="updateEe">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 8,
      total: 0,
      username: "",
      nickname: "",
      mobile: "",
      email: "",
      multipleSelection: [],
      options: ["国企", "私企"],
      quality: "",
      dialogVisible: false,
      editDialogVisible: false,
      formData: {
        username: "",
        password: "",
        nickname: "",
        mobile: "",
        email: "",
        address: "",
        quality: "",
        description: "",
        logo: "",
      },
      editData: {
        id: null,
        password: "",
        nickname: "",
        mobile: "",
        email: "",
        address: "",
        quality: "",
        description: "",
        logo: "",
      },
      rules: {
        nickname: [
          { required: true, message: "请输入企业名称", trigger: "blur" },
          {
            min: 3,
            max: 100,
            message: "长度在 3 到 100 个字符",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "请输入企业账号", trigger: "blur" },
          {
            min: 3,
            max: 50,
            message: "长度在 3 到 50 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 200,
            message: "长度在 3 到 200 个字符",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "请输入电话号", trigger: "blur" },
          {
            min: 8,
            max: 11,
            message: "长度在 8 到 11 个字符",
            trigger: "blur",
          },
        ],
        email: [
          { required: true, message: "请输入邮箱", trigger: "blur" },
          {
            min: 8,
            max: 20,
            message: "长度在 8 到 20 个字符",
            trigger: "blur",
          },
        ],
        address: [
          { required: true, message: "请输入地址", trigger: "blur" },
          {
            min: 2,
            max: 50,
            message: "长度在 2 到 50 个字符",
            trigger: "blur",
          },
        ],
        quality: [
          { required: true, message: "请选择公司性质", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    headers() {
      return { Authorization: sessionStorage.getItem("tokenStr") };
    },
  },
  created() {
    this.initEeInfo();
  },
  methods: {
    beforeAvatarUpload(file) {
      var testMsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (testMsg != "jpg" && testMsg != "jpeg" && testMsg != "png") {
        this.$message({
          message: "上传文件只能是jpg、jpeg、png格式",
          type: "error",
        });
        return false;
      }
      return true;
    },
    beforeAvatarUpload4E(file) {
      var testMsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      if (testMsg != "xls" && testMsg != "xlsx") {
        this.$message({
          message: "上传文件只能是xls、xlsx格式",
          type: "error",
        });
        return false;
      }
      return true;
    },
    handleAvatarSuccess4E(res) {
      if (res.code == 200) {
        this.$message.success(res.message);
        this.initEeInfo();
      }
    },
    handleAvatarSuccess(res) {
      if (res.code == 200) {
        this.$message.success(res.message);
        this.initEeInfo();
      }
    },
    show() {
      this.formData = {};
      this.dialogVisible = true;
    },
    clearInput() {
      location.reload()
    },
    // 表格样式设置
    rowClass() {
      return "text-align: center;";
    },
    headClass() {
      return "text-align: center;background:#f8f8f9;";
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.initEeInfo();
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.initEeInfo();
    },
    exportEeInfo() {
      this.$downloadReq("/enterprise/export");
    },
    handleDelete(row) {
      this.$confirm('此操作将删除"' + row.nickname + '", 是否继续?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.$delete("enterprise/?ids=" + row.id).then((res) => {
          if (res.code == 200) {
            this.initEeInfo();
          }
        });
      });
    },
    delAll() {
      this.$confirm(
        '此操作将删除"' + this.multipleSelection.length + '"个企业, 是否继续?',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        let ids = "?";
        this.multipleSelection.forEach((item) => {
          ids += "ids=" + item.id + "&";
        });
        this.$delete("enterprise/" + ids).then((res) => {
          if (res.code == 200) {
            this.initEeInfo();
          }
        });
      });
    },
    changeEnabled(id, nickname) {
      this.$confirm(
        "确定要" + '"停用"' + '"' + nickname + '"用户吗？',
        "系统提示",
        {
          confirmButtonText: "确定·",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          this.$post("enterprise/", { id: id, enabled: false }).then((res) => {
            if (res.code == 200) {
              this.initEeInfo();
            }
          });
        })
        .catch(() => {
          this.initEeInfo();
        });
    },
    changeDisable(id) {
      this.$post("enterprise/", { id: id, enabled: true }).then((res) => {
        if (res.code == 200) {
          this.initEeInfo();
        }
      });
    },
    addEe() {
      this.$refs["registerForm"].validate((valid) => {
        if (valid) {
          this.$post("enterprise/", this.formData).then((res) => {
            if (res.code == 200) {
              this.initEeInfo();
              this.dialogVisible = false;
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    updateEe() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          this.$post("enterprise/", this.editData).then((res) => {
            if (res.code == 200) {
              this.initEeInfo();
              this.editDialogVisible = false;
            }
          });
        } else {
          this.$message.error("数据校验失败，请检查");
        }
      });
    },
    handleEdit(row) {
      this.editData.id = row.id;
      this.editData.nickname = row.nickname;
      this.editData.email = row.email;
      this.editData.password = row.password;
      this.editData.description = row.description;
      this.editData.address = row.address;
      this.editData.mobile = row.mobile;
      this.editData.quality = row.quality;
      this.logo = row.logo;
      this.editDialogVisible = true;
    },
    initEeInfo() {
      this.$get(
        "enterprise/" +
          this.currentPage +
          "/" +
          this.pageSize +
          "?nickname=" +
          this.nickname +
          "&username=" +
          this.username +
          "&mobile=" +
          this.mobile +
          "&email=" +
          this.email +
          "&quality=" +
          this.quality
      ).then((res) => {
        if (res.code == 200 && res.code) {
          this.tableData = res.data.records;
          this.total = res.data.total;
        }
      });
    },
  },
};
</script>

<style>
</style>